package tests.utils;

import java.util.List;

import misc.DerivativeFunction;
import misc.Function;

import org.junit.Test;

import utils.math.MathCodes;

public class EquationSolverTest {

    @Test
    public void bisectionTest(){
	
	double[] polyCoef = { 0.2, -1.85, 3.2, 5.4, 0, 7.5 };
	
	double tolerance = 0.5 * Math.pow(10, -10);
	List<Double> result = MathCodes.bisection(polyCoef, -5, 5, tolerance);
	
	System.out.println("The Result is: "+ result.get(1));
	System.out.println("The mistake is: "+ result.get(0));
		
    }
    
    @Test
    public void newtonRapsonTest(){
	
	DerivativeFunction f = new DerivativeFunction(){

	    @Override
	    public Function getFirstDerivative() {
		return new Function(){

		    @Override
		    public double valueAtPoint(double point) {
			return -Math.sin(point) -3;
		    }
		    
		};
	    }

	    @Override
	    public Function getSecondDerivative() {
		
		return new Function(){

		    @Override
		    public double valueAtPoint(double point) {
			return -Math.cos(point);
		    }
		    
		};
	    }

	    @Override
	    public double valueAtPoint(double point) {
		
		return 1 + Math.cos(point) - 3 * point;
	    }
	    
	};
	
	double tolerance = 0.5 * Math.pow(10, -10);
	double result = MathCodes.NewtonRapson(f, 0, 1, tolerance);
	
	System.out.println("The Result is: "+ result);	
    }
    
    @Test
    public void secontTest(){
	
	DerivativeFunction f = new DerivativeFunction(){

	    @Override
	    public Function getFirstDerivative() {
		return new Function(){

		    @Override
		    public double valueAtPoint(double point) {
			return Math.pow(Math.tan(point), 2);
		    }
		    
		};
	    }

	    @Override
	    public Function getSecondDerivative() {
		
		return new Function(){

		    @Override
		    public double valueAtPoint(double point) {
			return 2 * Math.tan(point) * (1.0 / Math.pow(Math.cos(point), 2));
		    }
		    
		};
	    }

	    @Override
	    public double valueAtPoint(double point) {
		
		return Math.tan(point) - point;
	    }
	    
	};
	
	double tolerance = 0.5 * Math.pow(10, -10);
	double result = MathCodes.secont(f, 4.3, 0.3, tolerance);
	
	System.out.println("The Result is: "+ result);	
    }    
    
}
